use "$path\datasets\rd_dataset.dta", clear


keep if in_rdd_sample==1
foreach d of varlist bevoelkerung sum_elected_hcd tot_share_women_elected count_partei {
	bysort number_of_close_elec: egen m_`d'=mean( `d' )
	}



preserve
bysort gkz legislative_period partei: keep if _n==1
bysort number_of_close_elec: egen count =count(gkz)
tempfile temp
bysort number_of_close_elec: keep if _n==1
keep number_of_close_elec count
sort number_of_close_elec
save `temp', replace
restore



bysort number_of_close_elec: keep if _n==1
merge 1:1 number_of_close_elec using  `temp'
drop _merge


gen extra_women_effect= ((1/m_sum_elected_hcd))*100
gen rel_effect_on_fem_rep= (extra_women_effect/m_tot_share_women_elected)*100
gen individual_elections = count/number_of_close_elec


order number_of_close_elec  individual_elections m_bevoelkerung m_sum_elected_hcd m_count_partei m_tot_share_women_elected extra_women_effect rel_effect_on_fem_rep

keep number_of_close_elec  individual_elections m_bevoelkerung m_sum_elected_hcd m_count_partei m_tot_share_women_elected extra_women_effect rel_effect_on_fem_rep

lab var number_of_close_elec "Races"
lab var individual_elections "Elections"
lab var m_bevoelkerung "Population"
lab var m_sum_elected_hcd "Council size"
lab var m_count_partei "Parties"
lab var m_tot_share_women_elected "% Women"
lab var extra_women_effect "Delta % Women"
lab var rel_effect_on_fem_rep "% Effect female victory"


format m_bevoelkerung m_sum_elected_hcd m_count_partei m_tot_share_women_elected extra_women_effect rel_effect_on_fem_rep  %9.2fc


export excel  using "Table1/table1.xlsx", replace firstrow(varl)

import excel "Table1/table1.xlsx", clear



